package com.tarena.lbs.basic.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.basic.dao.mapper.AreaMapper;
import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@Slf4j
public class AreaRepositoryImpl implements AreaRepository {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<AreaVO> selectTree() {
        QueryWrapper<AreaPO> queryWrapper=new QueryWrapper();
        queryWrapper.gt("id",0);
        List<AreaPO>list=areaMapper.selectList(queryWrapper);
        log.info("listPO:{}", list);
        List<AreaVO>listVO=convertPoListToVoList(list);
        log.info("listVO:{}", list);
        return listVO;
    }
    public List<AreaVO> convertPoListToVoList(List<AreaPO> poList) {
        if (poList == null) {
            return new ArrayList<>();
        }

        return poList.stream()
                .map(this::convertPoToVo)
                .collect(Collectors.toList());
    }
    private AreaVO convertPoToVo(AreaPO po) {
        if (po == null) {
            return null;
        }

        AreaVO vo = new AreaVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }
}
